//
//  ComposeEmojiInputView.swift
//  WBSwift
//
//  Created by 贺瑞 on 2017/8/18.
//  Copyright © 2017年 herui. All rights reserved.
//

import UIKit

class ComposeEmojiInputView: UIView {
    
    var emoticons = Emoticons()
    
    var collectionView : UICollectionView?
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        定个数、行数
//        定间距
//        大小不定
        
        let colCount = 7
        let margin = CGFloat(20)
        let viewW = CGFloat(collectionView?.bounds.size.width ?? 0)
        let itemWH = (viewW - (CGFloat((colCount + 1)) * margin) ) / CGFloat(colCount)
        
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        layout.sectionInset = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
        layout.scrollDirection = .horizontal
        
    }
    
    
    
    
    func setupUI() {
        
        let collectionView = UICollectionView(frame: CGRect(), collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        collectionView.backgroundColor = UIColor.white
        collectionView.register(ComposeEmojiInputViewItem.classForCoder(), forCellWithReuseIdentifier: ComposeEmojiInputViewItem.reuseID)
        addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-100)
        }
        self.collectionView = collectionView
    }
    
    
}

extension ComposeEmojiInputView : UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return emoticons.data?[section]?.count ?? 0
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return emoticons.data?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ComposeEmojiInputViewItem.reuseID, for: indexPath) as! ComposeEmojiInputViewItem
        
        
        cell.emotion = emoticons.data?[indexPath.section]?[indexPath.row]
        
        return cell
    }
    
    
}


